package com.dao;

import com.utils.JDBCUnits;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AddFriendDao {
    /**
     * 加好友功能
     * @param user_id 自己的id
     * @param id 对方的id
     */
    public static void addFriend(int user_id, int id) {
        Connection c = null;
        PreparedStatement ps = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "insert into friendlist(user_id,friend_id,time) values(?,?,now())";
            ps = c.prepareStatement(sql);
            ps.setInt(1, user_id);
            ps.setInt(2, id);
            int i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUnits.close(c, ps);
        }
    }

    /**
     * 拒绝加好友
     * @param user_id
     * @param id
     * @param way
     */
    public static void deleteAdd(int user_id, int id, String way) {
        Connection c = null;
        PreparedStatement ps = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "delete from inbox where user_id=? and target_id=? and way=?";
            ps = c.prepareStatement(sql);
            ps.setInt(1, user_id);
            ps.setInt(2, id);
            ps.setString(3, way);
            int i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUnits.close(c, ps);
        }
    }
}
